package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataRailwayCorporationDTO;
import com.ctshk.rpc.system.entity.MainDataRailwayCorporation;
import com.ctshk.rpc.system.enums.ISCode;
import com.ctshk.rpc.system.mapper.MainDataRailwayCorporationMapper;
import com.ctshk.rpc.system.req.MainDataRailwayCorporationListReq;
import com.ctshk.rpc.system.req.MainDataRailwayCorporationReq;
import com.ctshk.rpc.system.service.IMainDataRailwayCorporationService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 铁路公司数据表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-04-01
 */
@DubboService
public class MainDataRailwayCorporationServiceImpl extends ServiceImpl<MainDataRailwayCorporationMapper, MainDataRailwayCorporation> implements IMainDataRailwayCorporationService {

    @Autowired
    private MainDataRailwayCorporationMapper mainDataRailwayCorporationMapper;

    @Override
    public Result list(MainDataRailwayCorporationListReq req, TokenUser currentUser) {
        QueryWrapper<MainDataRailwayCorporation> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MainDataRailwayCorporation::getIsDeleted, IsDeletedCode.NO.getCode());
//        wrapper.lambda().eq(MainDataRailwayCorporation::getStatus, ISCode.NO.getCode());
        wrapper.lambda().like(StringUtils.isNotBlank(req.getName()), MainDataRailwayCorporation::getRailwayCorporationName, req.getName());
        wrapper.lambda().eq(null != req.getStatus(), MainDataRailwayCorporation::getStatus, req.getStatus());
        IPage<MainDataRailwayCorporation> pageParam = new Page<>(req.getPageNo(), req.getPageSize());
        IPage<MainDataRailwayCorporation> pageResult = mainDataRailwayCorporationMapper.selectPage(pageParam, wrapper);
        List<MainDataRailwayCorporation> records = pageResult.getRecords();
        List<MainDataRailwayCorporationDTO> dtoList = EntityUtil.copyList(records, MainDataRailwayCorporationDTO.class);
        PageResponse<MainDataRailwayCorporationDTO> pageResponse = new PageResponse<>(dtoList,
                pageResult.getCurrent() == 1,
                pageResult.getTotal() <= (pageResult.getCurrent() * pageResult.getSize()),
                pageResult.getTotal(), pageResult.getCurrent(),
                pageResult.getSize());
        return Result.success(pageResponse);
    }

    @Override
    public Result detail(Long id, TokenUser currentUser) {
        QueryWrapper<MainDataRailwayCorporation> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MainDataRailwayCorporation::getId, id);
        wrapper.lambda().eq(MainDataRailwayCorporation::getIsDeleted, IsDeletedCode.NO.getCode());
        wrapper.lambda().eq(MainDataRailwayCorporation::getStatus, ISCode.NO.getCode());
        MainDataRailwayCorporation mainDataRailwayCorporation = mainDataRailwayCorporationMapper.selectOne(wrapper);
        MainDataRailwayCorporationDTO dto = EntityUtil.copy(mainDataRailwayCorporation, MainDataRailwayCorporationDTO.class);
        return Result.success(dto);
    }

    @Override
    public Result add(MainDataRailwayCorporationReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataRailwayCorporation mainDataRailwayCorporation = EntityUtil.copy(req, MainDataRailwayCorporation.class);
        mainDataRailwayCorporation.setId(IdWorker.getId());
        mainDataRailwayCorporation.setCreateId(tokenUser.getId());
        mainDataRailwayCorporation.setModifiedId(tokenUser.getId());
        mainDataRailwayCorporation.setModifiedName(tokenUser.getUsername());
        mainDataRailwayCorporation.setGmtCreate(now);
        mainDataRailwayCorporation.setGmtModified(now);
        mainDataRailwayCorporation.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(mainDataRailwayCorporation);
        return Result.success();
    }

    @Override
    public Result update(MainDataRailwayCorporationReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataRailwayCorporation mainDataRailwayCorporation = EntityUtil.copy(req, MainDataRailwayCorporation.class);
        mainDataRailwayCorporation.setModifiedId(tokenUser.getId());
        mainDataRailwayCorporation.setModifiedName(tokenUser.getUsername());
        mainDataRailwayCorporation.setGmtModified(now);
        baseMapper.updateById(mainDataRailwayCorporation);
        return Result.success();
    }
}
